// app.js
App({
  globalData: {
    userInfo: null,
    isLoggedIn: false,
    backgroundMusic: null,
    isMusicPlaying: false,
    favoriteRoutes: [],
    currentLocation: null,
    theme: 'light'
  },

  onLaunch() {
    console.log('小程序启动');
    
    // 检查登录状态
    this.checkLoginStatus();
    
    // 初始化背景音乐
    this.initBackgroundMusic();
    
    // 获取用户位置
    this.getUserLocation();
    
    // 从本地存储加载收藏路线
    this.loadFavoriteRoutes();
  },

  onShow() {
    console.log('小程序显示');
  },

  onHide() {
    console.log('小程序隐藏');
    // 暂停背景音乐
    if (this.globalData.backgroundMusic && this.globalData.isMusicPlaying) {
      this.globalData.backgroundMusic.pause();
    }
  },

  // 检查登录状态
  checkLoginStatus() {
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      this.globalData.userInfo = userInfo;
      this.globalData.isLoggedIn = true;
    }
  },

  // 初始化背景音乐
  initBackgroundMusic() {
    this.globalData.backgroundMusic = wx.createInnerAudioContext();
    this.globalData.backgroundMusic.src = '/audio/xiamen.mp3';
    this.globalData.backgroundMusic.loop = true;
    this.globalData.backgroundMusic.volume = 0.5;
    
    this.globalData.backgroundMusic.onPlay(() => {
      console.log('背景音乐开始播放');
      this.globalData.isMusicPlaying = true;
    });
    
    this.globalData.backgroundMusic.onPause(() => {
      console.log('背景音乐暂停');
      this.globalData.isMusicPlaying = false;
    });
    
    this.globalData.backgroundMusic.onError((res) => {
      console.log('背景音乐播放错误:', res);
    });
  },

  // 播放/暂停背景音乐
  toggleBackgroundMusic() {
    if (this.globalData.isMusicPlaying) {
      this.globalData.backgroundMusic.pause();
    } else {
      this.globalData.backgroundMusic.play();
    }
  },

  // 获取用户位置
  getUserLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        this.globalData.currentLocation = {
          latitude: res.latitude,
          longitude: res.longitude
        };
        console.log('获取位置成功:', res);
      },
      fail: (err) => {
        console.log('获取位置失败:', err);
      }
    });
  },

  // 加载收藏路线
  loadFavoriteRoutes() {
    const routes = wx.getStorageSync('favoriteRoutes') || [];
    this.globalData.favoriteRoutes = routes;
  },

  // 添加收藏路线
  addFavoriteRoute(route) {
    const routes = this.globalData.favoriteRoutes;
    const exists = routes.find(r => r.id === route.id);
    if (!exists) {
      routes.push(route);
      this.globalData.favoriteRoutes = routes;
      wx.setStorageSync('favoriteRoutes', routes);
      wx.showToast({
        title: '已添加到收藏',
        icon: 'success'
      });
    }
  },

  // 移除收藏路线
  removeFavoriteRoute(routeId) {
    const routes = this.globalData.favoriteRoutes.filter(r => r.id !== routeId);
    this.globalData.favoriteRoutes = routes;
    wx.setStorageSync('favoriteRoutes', routes);
    wx.showToast({
      title: '已移除收藏',
      icon: 'success'
    });
  },

  // 用户登录
  login(userInfo) {
    this.globalData.userInfo = userInfo;
    this.globalData.isLoggedIn = true;
    wx.setStorageSync('userInfo', userInfo);
  },

  // 用户登出
  logout() {
    this.globalData.userInfo = null;
    this.globalData.isLoggedIn = false;
    wx.removeStorageSync('userInfo');
  }
}); 